package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;
    @Autowired//云点播专属
    private VodService vodService;

    /**
     * 批量获取下单付费声音列表
     * @param trackId
     * @param trackCount
     * @return
     */
    @TsLogin
    @Operation(summary = "批量获取下单付费声音列表")
    @GetMapping("/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId, @PathVariable("trackCount") Integer trackCount){
        //  用户Id
        Long userId = AuthContextHolder.getUserId();
        //  调用服务层方法：
        List<TrackInfo> trackInfoList = trackInfoService.findPaidTrackInfoList(trackId, trackCount, userId);
        //  返回数据;
        return Result.ok(trackInfoList);
    }


    @TsLogin
    @Operation(summary = "获取声音分集购买列表")
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result findUserTrackPaidList(@PathVariable Long trackId){
        // 获取用户id
        Long userId = AuthContextHolder.getUserId();
        // 调用服务层方法
        List<Map<String,Object>> map = trackInfoService.findUserTrackPaidList(trackId, userId);
        // 返回数据
        return  Result.ok( map);
    }


    @TsLogin
    @Operation(summary = "分页查询专辑详情下的声音列表")
    @GetMapping("/findAlbumTrackPage/{albumId}/{pageNO}/{pageSize}")
    public Result findAlbumTrackPage(@PathVariable("albumId") Long albumId,
                                     @PathVariable("pageNO") Integer pageNO,
                                     @PathVariable("pageSize") Integer pageSize) {
        // 获取用户id
        Long userId = AuthContextHolder.getUserId();
        // 构建page对象
        Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pageNO, pageSize);
        // 调服务层
        IPage<AlbumTrackListVo> albumTrackListVoIPage = trackInfoService.findAlbumTrackPage(albumTrackListVoPage, albumId, userId);
        // 返回数据
        return Result.ok(albumTrackListVoIPage);
    }

    @Operation(summary = "修改声音")
    @PutMapping("/updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable("trackId") Long trackId,
                                  @RequestBody TrackInfoVo trackInfoVo) {
        //调用服务层方法
        trackInfoService.updateTrackInfo(trackId, trackInfoVo);
        //返回数据
        return Result.ok();
    }

    @Operation(summary = "根据声音id获取声音对象")
    @GetMapping("/getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable("trackId") Long trackId) {
        //调用服务层方法
        TrackInfo trackInfo = trackInfoService.getById(trackId);
        //  返回数据
        return Result.ok(trackInfo);
    }


    @Operation(summary = "删除声音")
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable("trackId") Long trackId) {
        //调用服务层方法
        trackInfoService.removeTrackInfo(trackId);
        //返回数据
        return Result.ok();
    }


    @Operation(summary = "上传声音文件到腾讯云点播")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        Map<String, Object> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    //http://localhost/api/album/trackInfo/saveTrackInfo
    @TsLogin
    @Operation(summary = "保存声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        //获取用户id
        Long userId = null == AuthContextHolder.getUserId() ? 1L : AuthContextHolder.getUserId();
        //调用服务层方法
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        //返回数据
        return Result.ok();
    }

    @TsLogin
    @Operation(summary = "分页查询当前用户id所有声音")
    @PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
    public Result findUserTrackPage(@PathVariable("pageNo") Integer pageNo,
                                    @PathVariable("pageSize") Integer pageSize,
                                    @RequestBody TrackInfoQuery trackInfoQuery) {
        // 构建page对象
        Page<TrackListVo> trackListVoPage = new Page<>(pageNo, pageSize);
        // 用户id
        Long userId = null == AuthContextHolder.getUserId() ? 1L : AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        // 调服务层
        IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserAllAlbumList(trackListVoPage, trackInfoQuery);

        // 返回数据
        return Result.ok(trackListVoIPage);
    }

}

